import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;

import gui.Clock;
import gui.EarthPanel;
import gui.PresentationControlPanel;
import gui.UserControlPanel;
import common.Constants;
import common.EarthSimSettings;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class EarthSim2 extends JFrame {
  private static final long serialVersionUID = 3386230071400230901L;

  private Container contentPane = getContentPane();
  
  /** Creates a new instance of GUIPlate */
  public EarthSim2(String[] options) {
    super("Earth Heat Simulation");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(new Dimension(Constants.FRAME_WIDTH, Constants.FRAME_HEIGHT));
    setLocation(100, 0);
    setOptions(options);
  }

  private void createAndShowGUI() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

    EarthPanel pnlEarth = new EarthPanel();
    Clock clock = new Clock(pnlEarth);
    UserControlPanel pnlControls = new UserControlPanel(pnlEarth, clock);
    pnlControls.setAlignmentX(Component.LEFT_ALIGNMENT);
    PresentationControlPanel pnlPresentation = new PresentationControlPanel(pnlEarth, clock);
    pnlPresentation.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
    contentPane.add(pnlControls);
    contentPane.add(pnlPresentation);
    contentPane.add(pnlEarth);
    setVisible(true);
  }

  public void setOptions(String[] options) {
    EarthSimSettings settings = EarthSimSettings.getInstance();
    for(int i = 0; i < options.length; i+=2) {
      if(options[i].equals(Constants.PRECISION)) {
        settings.setPrecision(Integer.parseInt(options[i+1]));
      }
      else if(options[i].equals(Constants.SAMPLING_RATE)) {
        settings.setSamplingRate(Integer.parseInt(options[i+1]));
      }
      else if(options[i].equals(Constants.TEMPORAL_ACCURACY)) {
        settings.setTemporalAccuracy(Integer.parseInt(options[i+1]));
      }
      else {
        System.out.println(
          "Invocation Parameters:"+
          "\n"+Constants.PRECISION+": The precision of the data to be stored, in decimal digits after the decimal point."+
          "\n\n"+Constants.SAMPLING_RATE+": The geographic accuracy (sampling rate) of the temperature data to be stored, as \nan integer percentage of the number of grid cells saved versus the number simulated."+
          "\n\n"+Constants.TEMPORAL_ACCURACY+": The temporal accuracy of the temperature data to be stored, as an integer percentage \nof the number of time periods saved versus the number computed."
        );
        System.exit(-1);
      }
    }
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    final EarthSim2 simulation = new EarthSim2(args);
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        simulation.createAndShowGUI();
      }
    });
  }
}
